﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Entities;
using DLTN.Data;
/// <summary>
/// @author: ThanhLT
/// </summary>
public partial class Controls_ControlMenuTop : System.Web.UI.UserControl
{

    public static string noLink = "Chưa có bài viết...";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initLinkSubMenu();
            // Hien thi tour trong nuoc
            hienthiTourTNTour();

            // Hien thi tour quoc te
            hienthiTourQuocTe();
            hienthiTourDoc();
            // Hien thi Tour doc
        }
    }

    /// <summary>
    /// Phuong thuc khoi tao link cho cac submenu
    /// </summary>
    public void initLinkSubMenu()
    {
        // Tour trong nuoc truoc
        subTourMb.NavigateUrl = getLinkSubTour(Common.TourTrongNuoc, Common.MienBac);
        subTourMt.NavigateUrl = getLinkSubTour(Common.TourTrongNuoc, Common.MienTrung);
        subTourMn.NavigateUrl = getLinkSubTour(Common.TourTrongNuoc, Common.MienNam);
        subTourDb.NavigateUrl = getLinkSubTour(Common.TourTrongNuoc, Common.TourDacBiet);
        dsTourdm1.NavigateUrl = "~/ListTour.aspx";

        // Tour Quoc te
        subTourCa.NavigateUrl = getLinkSubTour(Common.TourNuocNgoai, Common.TourChauAu);
        subTourCas.NavigateUrl = getLinkSubTour(Common.TourNuocNgoai, Common.TourChauA);
        subTourCm.NavigateUrl = getLinkSubTour(Common.TourNuocNgoai, Common.TourChauMy);
        subTourDb1.NavigateUrl = getLinkSubTour(Common.TourNuocNgoai, Common.TourDacBiet);
        dsTourdm2.NavigateUrl = "~/ListTour.aspx";

        // Tour doc dao
        subTourTh.NavigateUrl = getLinkSubTour(Common.TourDoc, Common.ExplorativeTour);
        subTourTm.NavigateUrl = getLinkSubTour(Common.TourDoc, Common.HoneyMoonTour);
        subTourcc.NavigateUrl = getLinkSubTour(Common.TourDoc, Common.HighUpTour);
        subTourDb2.NavigateUrl = getLinkSubTour(Common.TourDoc, Common.TourDacBiet);
        dsTourdm3.NavigateUrl = "~/ListTour.aspx";

        // Dich vu
        subKs.NavigateUrl = getLinkDiVu(Common.Hotels);
        subBh.NavigateUrl = getLinkDiVu(Common.Insurance);
        subTvdh.NavigateUrl = getLinkDiVu(Common.AbroadSupport);
        subXnc.NavigateUrl = getLinkDiVu(Common.Immigration);
        subVc.NavigateUrl = getLinkDiVu(Common.VanChuyen);
        subVmb.NavigateUrl = getLinkDiVu(Common.VeMayBay);

        // Media
        subTt.NavigateUrl = getLinkMedia(Common.TinTuc);
        subSk.NavigateUrl = getLinkMedia(Common.SuKien);
        subTcbc.NavigateUrl = getLinkMedia(Common.ThongCaoBaoChi);
        //subDknt.NavigateUrl = getLinkMedia(Common.DangKyNhanNewLetter);

        // Gioi thieu
        subHd.NavigateUrl = getLinkGioiThieu(Common.HoatDong);
        subQtt.NavigateUrl = getLinkGioiThieu(Common.ThanhTich);
        subLs.NavigateUrl = getLinkGioiThieu(Common.LichSu);
        subTd.NavigateUrl = getLinkGioiThieu(Common.TuyenDung);

    }

    /// <summary>
    /// Lay link cua SubTour dua tren phan loai tour va ten sub tour cua phan loai Tour
    /// </summary>
    /// <param name="idLoaiTour"></param>
    /// <param name="tenSubTour"></param>
    /// <returns></returns>
    public string getLinkSubTour(string idLoaiTour, string tenSubTour)
    {
        string link = "~/";

        int TotalRows = 0;
        TList<LoaiTour> TList_LoaiTour = DataRepository.LoaiTourProvider.GetPaged("TenLoaiTour like N'%" + idLoaiTour + "%'", "ID", 0, 0, out TotalRows);
        if (TList_LoaiTour.Count > 0)
        {
            string idLoai = TList_LoaiTour[0].Id.ToString();
            string WhereClause1 = "1=1";

            WhereClause1 += " and TenSubTour like N'%" + tenSubTour + "%' and IdLoaiTour in (select Id from LoaiTour where Id = " + idLoai + ")";
            TList<SubTour> TList_SubTour = DataRepository.SubTourProvider.GetPaged(WhereClause1, "ID", 0, 0, out TotalRows);
            if (TList_SubTour.Count > 0)
            {
                link += "SubListTour/" + TList_SubTour[0].Id + "/" + GetTieuDe(TList_SubTour[0].TenSubTour) + ".aspx";
            }
        }
        return link;
    }

    /// <summary>
    /// Phuong thuc thuc hien lay link cua cac Sub menu cua Dich vu
    /// </summary>
    /// <param name="loaiCm"></param>
    /// <returns></returns>
    public string getLinkDiVu(string loaiCm)
    {
        string link = "~/";
        int TotalRows = 0;
        string WhereClause = "1=1";

        TList<SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("TenSubChuyenMuc like N'%" + loaiCm + "%'", "ID", 0, 0, out TotalRows);
        if (TList_SubChuyenMuc.Count > 0)
        {
            WhereClause += " and trangthai =1 and IdSubChuyenMuc in (select Id from SubChuyenMuc where Id = " + TList_SubChuyenMuc[0].Id + " )";
            TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);
            link += "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + TList_SubChuyenMuc[0].IdLoaiChuyenMuc + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
        }

        return link;
    }

    /// <summary>
    /// Phuong thuc thuc hien lay link cua cac sub menu cua Media
    /// </summary>
    /// <param name="loaiMedia"></param>
    /// <returns></returns>
    public string getLinkMedia(string loaiMedia)
    {
        string link = "~/";

        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("TenSubChuyenMuc like N'%" + loaiMedia + "%'", "ID", 0, 0, out TotalRows);
        if (TList_SubChuyenMuc.Count > 0)
        {
            WhereClause += " and trangthai =1 and IdSubChuyenMuc in (select Id from SubChuyenMuc where Id = " + TList_SubChuyenMuc[0].Id + " )";
            TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);
            link += "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + TList_SubChuyenMuc[0].IdLoaiChuyenMuc + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
        }
        return link;
    }

    /// <summary>
    /// Phuong thuc thuc hien lay link cua cac sub menu cua Gioi Thieu
    /// </summary>
    /// <param name="gioiThieu"></param>
    /// <returns></returns>
    public string getLinkGioiThieu(string gioiThieu)
    {
        string link = "~/";

        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("TenSubChuyenMuc like N'%" + gioiThieu + "%'", "ID", 0, 0, out TotalRows);
        if (TList_SubChuyenMuc.Count > 0)
        {
            WhereClause += " and trangthai =1 and IdSubChuyenMuc in (select Id from SubChuyenMuc where Id = " + TList_SubChuyenMuc[0].Id + " )";
            TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);
            link += "SubChuyenMuc/" + TList_SubChuyenMuc[0].Id + "/" + TList_SubChuyenMuc[0].IdLoaiChuyenMuc + "/" + GetTieuDe(TList_SubChuyenMuc[0].TenSubChuyenMuc) + ".aspx";
        }
        return link;
    }

    /// Tour trong nuoc
    #region TOUR TRONG NUOC
    /// <summary>
    /// Tour trong nuoc
    /// </summary>
    public void hienthiTourTNTour()
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<LoaiTour> TList_LoaiTour = DataRepository.LoaiTourProvider.GetPaged("TenLoaiTour like N'%" + Common.TourTrongNuoc + "%'", "ID", 0, 0, out TotalRows);
        if (TList_LoaiTour.Count > 0)
        {
            TList<SubTour> TList_SubTour = DataRepository.SubTourProvider.GetPaged("IdLoaiTour=" + TList_LoaiTour[0].Id.ToString(), "ID", 0, 0, out TotalRows);
            if (TList_SubTour.Count > 0)
            {
                WhereClause += " and IdSubTour =" + TList_SubTour[0].Id + "or IdSubTour=" + TList_SubTour[1].Id + "or IdSubTour=" + TList_SubTour[2].Id + "or IdSubTour=" + TList_SubTour[3].Id;
                TList<Tour> TList_Tour = DataRepository.TourProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 4, out TotalRows);
                dlTrongNuoc.DataSource = TList_Tour;
                dlTrongNuoc.DataBind();
            }
        }
    }

    #endregion
    /// Tour Quoc Te
    #region TOUR QUOC TE
    /// Tour Quoc Te
    public void hienthiTourQuocTe()
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<LoaiTour> TList_LoaiTour = DataRepository.LoaiTourProvider.GetPaged("TenLoaiTour like N'%" + Common.TourNuocNgoai + "%'", "ID", 0, 0, out TotalRows);
        if (TList_LoaiTour.Count > 0)
        {
            TList<SubTour> TList_SubTour = DataRepository.SubTourProvider.GetPaged("IdLoaiTour=" + TList_LoaiTour[0].Id.ToString(), "ID", 0, 0, out TotalRows);
            if (TList_SubTour.Count > 0)
            {
                WhereClause += " and IdSubTour =" + TList_SubTour[0].Id + "or IdSubTour=" + TList_SubTour[1].Id + "or IdSubTour=" + TList_SubTour[2].Id + "or IdSubTour=" + TList_SubTour[3].Id;
                TList<Tour> TList_Tour = DataRepository.TourProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 4, out TotalRows);
                dlQuocTeTour.DataSource = TList_Tour;
                dlQuocTeTour.DataBind();
            }
        }
    }

    #endregion

    #region TOUR DOC
    /// <summary>
    /// Tour Doc
    /// </summary>
    public void hienthiTourDoc()
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<LoaiTour> TList_LoaiTour = DataRepository.LoaiTourProvider.GetPaged("TenLoaiTour like N'%" + Common.TourDoc + "%'", "ID", 0, 0, out TotalRows);
        if (TList_LoaiTour.Count > 0)
        {
            TList<SubTour> TList_SubTour = DataRepository.SubTourProvider.GetPaged("IdLoaiTour=" + TList_LoaiTour[0].Id.ToString(), "ID", 0, 0, out TotalRows);
            if (TList_SubTour.Count > 0)
            {
                WhereClause += " and IdSubTour =" + TList_SubTour[0].Id + "or IdSubTour=" + TList_SubTour[1].Id + "or IdSubTour=" + TList_SubTour[2].Id + "or IdSubTour=" + TList_SubTour[3].Id;
                TList<Tour> TList_Tour = DataRepository.TourProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 4, out TotalRows);
                dlDocTour.DataSource = TList_Tour;
                dlDocTour.DataBind();
            }
        }
    }

    #endregion

    /// <summary>
    /// Phuong thuc convert tieu de (Tieng-viet co dau) sang (Tieng-viet khong dau)
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
}